package com.big.merchan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.big.common.utils.DateUtils;
import com.big.merchan.mapper.MerchandiseInfoMapper;
import com.big.merchan.service.MerchandiseInfoService;
import com.big.merchan.service.MerchandiseIntegralService;
import com.big.merchan.service.MerchandisePhotoCordService;
import com.big.merchan.service.MerchandiseSpikeService;
import com.big.model.model.merchandise.*;
import com.big.model.vo.merchandise.MerchanQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MerchandiseInfoServiceImpl extends ServiceImpl<MerchandiseInfoMapper, MerchandiseInfo> implements MerchandiseInfoService {

    @Autowired
    MerchandisePhotoCordService merchandisePhotoCordService;


    @Autowired
    MerchandiseIntegralService merchandiseIntegralService;

    @Autowired
    MerchandiseSpikeService merchandiseSpikeService;

    /**
     *  商品详情
     */
    @Override
    public MerchandiseInfo findInfoMerchandise(Long id) throws ParseException {
        QueryWrapper<MerchandiseInfo> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(id)){
            queryWrapper.eq("id",id);
        }
        MerchandiseInfo info=baseMapper.selectOne(queryWrapper);
        this.PhotoCordInfos(info); //照片组
        this.FindIntegral(info);  //积分
        this.merchandiseSpikeInfo(info);  //秒杀信息
        return info;
    }


    /**
     * 添加商品详情
     * @param merchandiseInfo
     * @return
     */
    @Override
    public Integer SaveMerchandiseInfo(MerchandiseInfo merchandiseInfo) {
        int insert = baseMapper.insert(merchandiseInfo);
        return insert;
    }


    /**
     * 照片组
     * @param merchandiseInfo
     * @return
     */
   public MerchandiseInfo PhotoCordInfos(MerchandiseInfo merchandiseInfo){
       List<MerchandisePhotoCord> merchandisePhotoCords = merchandisePhotoCordService.merchandisePhotoCordInfos(String.valueOf(merchandiseInfo.getId()));
       merchandiseInfo.getParam().put("photoCor",merchandisePhotoCords);
       return merchandiseInfo;
   }

    /**
     * 积分
     * @param merchandiseInfo
     * @return
     */
    public MerchandiseInfo FindIntegral(MerchandiseInfo merchandiseInfo){
        MerchandiseIntegral integral = merchandiseIntegralService.findIntegral(String.valueOf(merchandiseInfo.getId()));
        System.out.println(integral);
        if(integral!=null){
            merchandiseInfo.getParam().put("Handsel",integral.getHandsel());
        }
        return merchandiseInfo;
    }



    public MerchandiseInfo  merchandiseSpikeInfo(MerchandiseInfo merchandiseInfo) throws ParseException {

        MerchandiseSpike merchandiseSpike = merchandiseSpikeService.MerchandiseSpikeInfo(String.valueOf(merchandiseInfo.getId()));
        if(merchandiseSpike!=null){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            switch (merchandiseSpike.getStatus()) {
            case 0:
                merchandiseInfo.getParam().put("statusName", "准备开始");
                String startTime=sdf.format(merchandiseSpike.getStartTime());
                Long aLong = DateUtils.msUtils(DateUtils.getDate(), startTime);
                merchandiseInfo.getParam().put("countdown",aLong);
                break;
            case 1:
                merchandiseInfo.getParam().put("statusName", "已开始");
                String alreadyTime=sdf.format(merchandiseSpike.getStartTime());
                String endTime=sdf.format(merchandiseSpike.getEndTime());
                Long LongTime = DateUtils.msUtils(alreadyTime, endTime);
                merchandiseInfo.getParam().put("countdown",LongTime);
                break;
            case 2:
                merchandiseInfo.getParam().put("statusName", "已结束");
                break;
        }
        }
        return merchandiseInfo;
    }


}
